// main.cpp（确保这是唯一的主文件）
#include <Arduino.h>
#include "wifi_control.h"
#include "ota_manager.h"
#include <WiFi.h>
#include <ThingsBoardClient.h>
#include <LittleFS.h>
#include "ASRModule.h"

// 全局对象（仅在main.cpp中定义一次）
WiFiControl wifiControl;
OTAManager otaManager;
HardwareSerial SerialPort(2); // UART2实例
ASRModule asrModule(SerialPort); // 通过SerialPort实例化ASRModule

// ThingsBoard配置保持不变
const String THINGSBOARD_SERVER_IP = "192.168.1.42";
const int THINGSBOARD_PORT = 8181;
const String DEVICE_TOKEN = "ae484420-1ac0-11f0-8e0b-213cf6ee0817";
const String DEVICE_NAME = "esp32_mp";
ThingsBoardClient thingsBoardClient(DEVICE_TOKEN, THINGSBOARD_SERVER_IP, THINGSBOARD_PORT, DEVICE_NAME);

// 语音指令回调函数
void onASRCommand(const String& command) {
    Serial.println("Received Voice Command: ");
    Serial.printf("Received Voice Command: %s\n", command.c_str());
}

void setup() {
    Serial.begin(115200);
    wifiControl.begin();
    otaManager.begin("30243024");
    thingsBoardClient.begin();
    Serial.println("System Ready");
    thingsBoardClient.sendDeviceInfo();

    // 初始化UART引脚和波特率
    SerialPort.begin(115200, SERIAL_8N1, 18, 19); // 确保引脚与硬件连接一致
    asrModule.begin(115200); // 使用SerialPort的配置
    asrModule.setCommandCallback(onASRCommand);
}

void loop() {
    wifiControl.update();
    otaManager.handle();
    asrModule.loop(); // 处理语音模块

    // 设备信息发送逻辑保持不变
    static unsigned long lastSendTime = 0;
    unsigned long currentTime = millis();
    if (currentTime - lastSendTime >= 1000*10) {
        lastSendTime = currentTime;
        if (WiFi.status() == WL_CONNECTED) {
            thingsBoardClient.sendDeviceInfo();
        } else {
            Serial.println("WiFi not connected, cannot send telemetry.");
        }
    }
}